package com.qf.filterimpl;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheFeign;
import com.qf.filter.FilterChainManager;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 * 执行策略
 *
 * @author Ypl
 * @version V1.0
 * @Project OctoberAndNovember
 * @Package com.qf.filterimpl
 * @Description:
 * @Date 2022/11/10 10:42
 */
@SuppressWarnings("all")
@Service
@Slf4j
public class FilterChainManagerImpl implements FilterChainManager {

    @Autowired
    Map<String, FilterChainService> filterMap;

    @Autowired
    CacheFeign cacheFeign;

    @Override
    public void executerFilter(StandardSubmit standardSubmit) {
        log.info("standardSubmit : {}" ,filterMap);

        //远程调用 获取redis中的客户的过滤器
        Map map = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_CLIENT + standardSubmit.getClientID());
        //根据key获取对应的策略
        String filtersArr = String.valueOf(map.get("filters"));
        String[] split = filtersArr.split(",");


        if(split !=null && split.length>0 ){
            //遍历得到的策略顺序
            for (String filters : split) {
                //然后根据策略顺序去执行对应的策略
                FilterChainService filtersService = filterMap.get(filters);
                //执行策略
                boolean flag = filtersService.strategy(standardSubmit);
                //如果策略执行失败就退出循环，不执行下面的策略
                if(!flag){
                    break;
                }
            }
        }
    }
}
